from django.http import HttpResponse
from django.shortcuts import render
from django.views import generic
from django.http import JsonResponse

from . import utils
from . import models


class HomePageDisplay(generic.View):

    """ Отображение домашней страницы """

    def get(self, request):
        return render(request, 'main/home_page.html')


class LibraryDisplay(generic.View):

    """ Отображение библиотеки """

    def get(self, request):
        books = models.Book.objects.all()
        context = {'block': 'library',
                   'books': books}

        return render(request, 'main/library.html', context=context)


class BookDisplay(generic.View):

    """ Отображение книги """

    def get(self, request, slug):

        template, context = utils.get_book(request, slug)

        if request.GET.get('update'):
            return JsonResponse(context)
        elif request.GET.get('information'):
            context = utils.get_information_about_book(request, slug)
            return JsonResponse(context)
        else:
            return render(request, template, context)


class BooksDisplay(generic.View):

    """ Отображаем список книг """

    def get(self, request):
        result = utils.get_list_of_books()
        context = {'books': result}
        return render(request, 'list_of_books.html', context)


class BookBookmark(generic.View):

    def post(self, request, book, action, page):
        utils.add_bookmark(request, book, action, page)
        return HttpResponse('')


class AddingLastPage(generic.View):

    def get(self, request, book, page_number):
        utils.add_last_page(request, book, page_number)
        return HttpResponse('')


class BookFavorites(generic.View):

    def post(self, request, book, action):
        utils.action_favorites(book, action)
        return HttpResponse('')
